package com.bw.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bw.domain.SysUser;
import com.bw.service.SysUserService;
import com.bw.utils.common.JWTUtils;
import com.bw.utils.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class LoginController {
    @Autowired
    SysUserService sysUserService;


    @RequestMapping("login")
    public Result getlogin(@RequestBody SysUser user)
    {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getUname,user.getUname());
        SysUser sysUser = sysUserService.getOne(lambdaQueryWrapper);
        if(sysUser==null)
        {
            return  Result.fail("账号不存在");
        }
        if(!sysUser.getPassword().equals(user.getPassword()))
        {
            return  Result.fail("密码不存在");
        }

        String token = JWTUtils.createToken(sysUser.getId(), 5 * 30);

        return Result.success(token);


    }

}
